import axios from 'axios'
// import Bus from './bus'
/**
 * 生成GUID
 *
 * @return {*} 
 */
 var Guid = function () {
    var guid = '';
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += '-';
    }
    return guid;
};


// import { GetUIStore } from '@/store/UI';
import { notification } from 'ant-design-vue'

// const UIStore = GetUIStore();


var StatusCodeMapping = {}

// http request 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.StatusCode == 0) {
            return response
        } else {
            var ErrorMessage = null;
            StatusCodeMapping.hasOwnProperty(response.data.StatusCode) && (ErrorMessage = StatusCodeMapping[response.data.StatusCode])
            notification.error({ message: 'StatusCode：' + response.data.StatusCode + ' ', description: ErrorMessage || response.data.Reason });
            return Promise.reject(error)
        }
    },
    //接口错误状态处理，也就是说无响应时的处理
    error => {
        if (error.response.status == 403) {
            // Bus.$emit('Illegalcalls');
            notification.error({ message: 'Service Error 403', description: '服务器出现错误，请检查参数信息' });
        } else if (error.response.status == 400) {
            notification.error({ message: 'Service Error 400', description: '参数错误，请检查参数信息' });
        } else if (error.response.status == 404) {
            notification.error({ message: 'Service Error 404', description: '未找到接口，请检查链接地址' });
        } else if (error.response.status == 500) {
            notification.error({ message: 'Service Error 500', description: '服务器出现错误' });
        } else if (error.response.status == 502) {
            notification.error({ message: 'Service Error 502', description: '服务器响应出现错误' });
        } else {
            notification.error({ message: 'Service Error' + error.response.status, description: '链接异常，请检查错误信息' });
        }
        return Promise.reject(error) // 返回接口返回的错误信息
    })

let AxiosEventCatch = {};

/**
 * 隐藏 loading 方法
 *
 * @param {*} id
 * @memberof Request
 */
const hideLoading = function (id) {
    delete AxiosEventCatch[id];
    if (Object.keys(AxiosEventCatch).length == 0) {
        // UIStore.loading = false;
    }
}

const Event = function (options) {
    var id = Guid();
    if (options.loading !== false) {
        AxiosEventCatch[id] = '';
        // UIStore.loading = true;
    }
    var baseUrlPath = ''
    try {
        baseUrlPath = window.WIN_COMFIG.service[options.reqType]
    } catch (error) {
        baseUrlPath = window.WIN_COMFIG.service.pro
    }

    var _url = baseUrlPath + options.url;

    var _params = JSON.parse(JSON.stringify(options.params));
    _url = _url.replace(/\{\w+\}/g, function (i) {
        var _src = i.replace('{', '').replace('}', '');
        if (_params.hasOwnProperty(_src)) {
            var _value = _params[_src];
            delete _params[_src];
            return _value;
        }
    });

    return axios({
        method: options.type,
        url: _url,
        params: _params,
        data: options.data
    }).then(function (res) {
        if (options.loading !== false) {
            hideLoading(id);
        }
        return res.data.Result;
    }).catch(function (err) {
        if (options.loading !== false) {
            hideLoading(id);
        }
        throw err.response
    });
}

export default {

    /**
     * apis 接口调用参数
     *
     * @param {String} url  链接地址
     * @param {Object} params Url 传参对象
     * @param {Object} data PostData 传参对象
     * @param {String} reqType 开发模式下, 链接地址指向
     * @param {Blob} loading 是否显示loading 开关
     * @return {axios} 接口异步对象
     */
    post(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'post';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    get(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'get';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    put(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'put';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    },
    delete(url, params, data, reqType, loading) {
        var _data = {};
        _data.type = 'delete';
        _data.data = data || {};
        _data.reqType = reqType;
        _data.params = params;
        _data.url = url;
        _data.loading = loading
        return Event(_data)
    }
}


